<template>
  <div class="goods-tabs">
    <nav>
      <a :class="{active:activeName==='GoodsDetail'}" href="javascript:;" @click="GoodsDetail">商品详情</a>
      <a :class="{active:activeName==='GoodsComment'}" href="javascript:;" @click="GoodsComment">商品评价<span>({{ goods.commentCount }})</span></a>
    </nav>
    <!-- 切换内容的地方 :两个组件-->
    <!--再vue中动态切换组件，可以使用动态组件，叫component-->
    <!--is属性绝对component动态组件渲染那个组件，组件名称-->
    <!--<GoodsDetail v-if="activeName==='GoodsDetail'"/>-->
    <!--<GoodsComment v-if="activeName==='GoodsComment'"/>-->
    <keep-alive>
      <component :is="activeName"></component>
    </keep-alive>
  </div>
</template>

<script>
import { inject, ref } from 'vue'
import GoodsDetail from '@/views/goods/components/goods-detail'
import GoodsComment from '@/views/goods/components/goods-comment'

export default {
  name: 'GoodsTabs',
  components: {
    GoodsComment,
    GoodsDetail
  },
  setup () {
    // 接收goods
    const goods = inject('goods')
    //  activeName的值：GoodsDetail（商品详情），GoodsComment（商品评论）
    const activeName = ref('GoodsDetail')
    const GoodsDetail = () => {
      activeName.value = 'GoodsDetail'
    }
    const GoodsComment = () => {
      activeName.value = 'GoodsComment'
    }
    return {
      goods,
      activeName,
      GoodsDetail,
      GoodsComment
    }
  }
}
</script>

<style lang="less" scoped>
.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }

      &:first-child {
        border-right: 1px solid #f5f5f5;
      }

      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @xtxColor;
        }
      }
    }
  }
}
</style>
